//
//  ChallengeYourselfViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 16/9/23.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "ChallengeYourselfViewController.h"
#import "DistanceSelectorView.h"

@interface ChallengeYourselfViewController ()<DistanceSelectorViewDelegate>
@property (strong, nonatomic) ChallengeModel *viewModel;
@property (strong, nonatomic) DistanceSelectorView *distanceSelectorView;
@end

@implementation ChallengeYourselfViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"自我挑战";
    [self showNavgationWithLeftItemWithTitle:@"首页"];
    UIImageView *imageView= [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_movement.jpg"]];
    [self.tableView setBackgroundView:imageView];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bigShadow.png"] forBarMetrics:UIBarMetricsDefault];    
    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
    _viewModel = [[ChallengeModel alloc] initWithChallengeModel:self.view];
}

- (void)viewWillDisappear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"home_top2_bg"] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_viewModel.challengeYourselfType == ChallengeYourselfTypeHome) {
        return 3;
    }else{
        return 2;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        static NSString *ID = @"ChallengeYourselfcell1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        return cell;
    }else{
        if (_viewModel.challengeYourselfType == ChallengeYourselfTypeHome) {
            if (indexPath.row == 1) {
                static NSString *ID = @"ChallengeYourselfcell3";
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
                if (cell == nil) {
                    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
                }
                return cell;
            }else{
                static NSString *ID = @"ChallengeYourselfcell4";
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
                if (cell == nil) {
                    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
                }
                return cell;
            }
        }else{
            static NSString *ID = @"ChallengeYourselfcell2";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
            }
            UIView *bottonView = [cell.contentView viewWithTag:1];
            [_distanceSelectorView removeFromSuperview];
            _distanceSelectorView = [[DistanceSelectorView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bottonView.frame), CGRectGetHeight(bottonView.frame))];
            _distanceSelectorView.deleaget = self;
            [bottonView addSubview:_distanceSelectorView];
            if (_viewModel.setType == SetTypeDistance) {
                [_distanceSelectorView initCard:ShowTypeDistance];
            }else if (_viewModel.setType == SetTypeTime){
                [_distanceSelectorView initCard:ShowTypeTime];
            }else{
                [_distanceSelectorView initCard:ShowTypeHeat];
            }
            [_viewModel initSetView:cell];
            return cell;
        }
    }
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 208;
    }else{
        if (_viewModel.challengeYourselfType == ChallengeYourselfTypeHome) {
            if (indexPath.row == 1) {
                return 180;
            }else{
                return 132;
            }
        }else{
            return 248;
        }
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(_viewModel.challengeYourselfType == ChallengeYourselfTypeHome){
        if(indexPath.row == 1){
            cell.contentView.alpha = 0.0;
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
            [UIView setAnimationDuration:1.5];
            [UIView setAnimationDelegate:self];
            cell.contentView.alpha = 1.0;
            [UIView commitAnimations];
        }else if(indexPath.row == 2){
            [NSThread sleepForTimeInterval:0.1f];
            cell.contentView.alpha = 0.0;
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
            [UIView setAnimationDuration:1.5];
            [UIView setAnimationDelegate:self];
            cell.contentView.alpha = 1.0;
            [UIView commitAnimations];
        }
    }else {
        if(indexPath.row == 1){
            cell.contentView.alpha = 0.0;
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
            [UIView setAnimationDuration:0.8];
            [UIView setAnimationDelegate:self];
            cell.contentView.alpha = 1.0;
            [UIView commitAnimations];
        }
    }
  
}


#pragma makr - DistanceSelectorViewDelegate
- (void)backSelect:(float)number{
    _viewModel.number = [NSString stringWithFormat:@"%.1f",number];
}

#pragma mark- 私有方法



- (void)backTo{
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
